﻿var IconA = "#fEffect/CharacterEff/1112903/0/0#"; //红心
var IconB = "#fEffect/CharacterEff/1112904/0/0#"; //彩心
var IconC = "#fEffect/CharacterEff/1112924/0/0#"; //黄星
var IconD = "#fEffect/CharacterEff/1112925/0/0#"; //蓝星
var IconE = "#fEffect/CharacterEff/1112926/0/0#"; //红星
var IconF = "#fUI/RunnerGame.img/RunnerGameUI/UI/Point/2#"
var IconG = "#fUI/SoulUI.img/DungeonMap/icon/dungeonItem/0#"
var head = "#fUI/UIWindow2.img/Quest/quest_info/summary_icon/summary#\r\n";
var EventStop;
status = -1;
var event;
var txt;
function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (status == 0 && mode == 0) {
        cm.dispose();
        return;
    }
    if (mode == 1) {
        status++;
    } else {
        status--;
    }
    event = cm.getEventManager("Gailou"); //获取活动脚本的名称 test 对应 event 目录里面的 gailou.js 文件
    if (status == 0) {
        em = cm.getEventManager("Gailou");
        if (event == null) {
            cm.sendOk("活动脚本错误...请联系管理员修复！或重新打开。");
            cm.dispose();
        } else if (cm.getPlayer().getClient().getChannel() != 1) {
            cm.sendOk("活动只能在1频道进行！亲！");
            cm.dispose();
        } else if (event != null && event.getProperty("state").equals("true")) {
            cm.sendYesNo("亲爱的#r#h ##k您好，我是盖楼活动员，本次活动时间为10分钟.\r\n活动分一等奖，二等奖和三等奖.\r\n一等奖：第一个到达建楼高度的玩家获得一等奖。\r\n二等奖：一等奖之后后续补楼的10个玩家为二等奖\r\n三等奖：为结束活动奖励只限1人随机获得 0 - 2000抵用卷\r\n那就看你运气啦 开始吧？");
        } else {
            EventStop = "#r当前状态 ： #d[关闭状态]#k";

            //-----------------------------------------------------
            if (em.getProperty("start") == "1") {//活动开放 
                EventStop = "#r当前状态 ：[开放状态]#k";
            }
            txt = "#n#r┄┄┄┅═☆#e" + IconF + "[ 抢楼活动中心 ]" + IconF + "#n☆═┅┄┄┄\r\n\r\n";
            txt += " " + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "\r\n\r\n";
            txt += " #r" + IconA + " " + EventStop + "\r\n";
            txt += " #r" + IconA + " #b活动结束后会立即颁布奖品 分别 第一 第二 第三\r\n";
            txt += " #r" + IconA + " 第一名 ▲ #d第一个到达最高层便是第一名\r\n";
            txt += " #r" + IconA + " 第二名 ▲ #d第一名后续十名依次列为第二名\r\n";
            txt += " #r" + IconA + " 第三名 ▲ #d活动结束限一名玩家 随机 0 - 2000 抵用卷\r\n\r\n";
            txt += " " + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "" + IconC + "" + IconD + "" + IconE + "\r\n";
            txt += "\t　#i5062009#　#i5062010#　#i5062002#　#i5064000#　#i2430692#";
            cm.sendOkS(txt, 2);
            cm.dispose();
        }
    } else if (status == 1) {
        if (event != null && event.getProperty("state").equals("true")) {
            event.setProperty("check", "" + (parseInt(event.getProperty("check")) + 1)); //设置点击次数+1
            var count = parseInt(event.getProperty("check")); //获得总点击次数
            var max = parseInt(event.getProperty("maxCheck"));
            var dj = rand(1000, 3000);
            var dj3 = rand(250, 800);
            if (count == max) {
                setEnergyvalues(1);//增加排名
                cm.gainNX(2, 5000);
                cm.worldMessage("[抢楼活动]： 恭喜玩家 " + cm.getName() + " 在抢楼活动中获得一等奖,真让人羡慕");
                cm.sendOk("[抢楼活动] 恭喜你获得了抢楼活动一等奖。");
            } else if (count > max && count <= (max + 30)) {
                cm.gainNX(2, dj);
                cm.worldMessage("[抢楼活动]： 恭喜玩家 " + cm.getName() + " 在抢楼活动中获得二等奖,真让人羡慕");
                cm.sendOk("恭喜你获得了抢楼活动二等奖。");
            } else if (count > (max + 20)) {
                cm.gainNX(2, dj3);
                event.setProperty("state", "false");
                event.setProperty("endEvent", "true");
                cm.worldMessage("[抢楼活动]： 恭喜玩家 " + cm.getName() + " 在抢楼活动中获得三等奖 " + dj3 + "抵用卷.本次抢楼活动已经结束...");
                cm.sendOk(head+"\r\n"+"恭喜你获得了抢楼活动三等奖。\r\n奖金 0 - 2000 点抵用卷不等。\r\n本次抢楼活动已经结束...");
            } else {
                cm.sendOk("当前楼层: " + parseInt(event.getProperty("check")) + " 楼。");
            }
        } else {
            cm.sendOk(head+"\r\n"+"活动还未开启或者活动已经结束，所有奖励均已经发放，请下次在参加。");
        }
        cm.dispose();
    }
}

function rand(lbound, ubound) {
    return Math.floor(Math.random() * (ubound - lbound)) + lbound;
}

function getEnergyvalue() {
    var CharData = cm.getConnection().prepareStatement("SELECT * FROM characters where id = " + cm.getPlayer().getId() + "").executeQuery();
    while (CharData.next()) {//得到记录数据
        return parseInt(CharData.getString("glpm"));
    }
    CharData.close();
}

function setEnergyvalues(Number) {
    var i = 0;
    var Times = cm.getConnection().prepareStatement("SELECT * FROM characters where id = " + cm.getPlayer().getId() + "").executeQuery();
    while (Times.next()) {
        i++;
    }
    if (i == 0) {
        var insert = cm.getConnection().prepareStatement("INSERT INTO characters VALUES(?,?)");
        insert.setString(cm.getPlayer().getId(), Number);
        insert.executeUpdate();
    } else {
        var update = cm.getConnection().prepareStatement("update characters set glpm = ? where id = " + cm.getPlayer().getId());
        update.setString(1, getEnergyvalue(cm.getPlayer().getId()) + Number);
        update.executeUpdate();
    }
}